package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Admin;
import com.easy.mapper.AdminDao;
import com.easy.service.LoginService;
import com.easy.util.PasswordUtil;
import com.easy.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    AdminDao adminDao;

    @LogMethod(serviceName = "登录信息",recordParam = true,recordReturn = true)
    @Override
    public Admin logininfo(String token) {
        //获取用户名
        Object member = TokenUtil.getOption(token, "member");

        Admin admin = adminDao.getByMember(member.toString());
        return admin;
    }

    @LogMethod(serviceName = "登录",recordParam = true,recordReturn = true)
    @Override
    public String login(Admin admin) {
        //登录逻辑
        //1.通过用户名查询数据库


        Admin result = adminDao.getByMember(admin.getMember());
       /* System.out.println(admin.getMember());*/
        if (result == null){
            throw new RuntimeException("用户不存在");
        }
        //2.验证密码
        String password=admin.getPassword();
        String encryptionPassword=result.getPassword();
        if (!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("密码错误");
        }
        //3.返回结果token
        String token= TokenUtil.getToken(result.getMember());

        return token;
    }

    @LogMethod(serviceName = "修改密码",recordParam = true,recordReturn = true)
    @Override
    public Admin edit(Admin admin) {
        //修改密码
        //1.验证密码
        //2.修改密码
        int result = adminDao.edit(admin);
        if (result==0){
            throw new RuntimeException("修改失败");
        }
        return admin;
    }

}
